import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * 替换段落中的换行符
 */
public class Main extends Application {

    public static final String LAST_STRING = "\n";
    public static final String NEW_STRING = "";

    private TextArea textArea1, textArea2;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10));
        gridPane.setVgap(10);
        gridPane.setHgap(10);

        textArea1 = new TextArea();
        textArea1.setWrapText(true);
        textArea1.setFont(Font.font("微软雅黑", 16));
        textArea2 = new TextArea();
        textArea2.setWrapText(true);
        textArea2.setFont(Font.font("微软雅黑", 16));
        gridPane.setVgrow(textArea1, Priority.ALWAYS);
        gridPane.setVgrow(textArea2, Priority.ALWAYS);

        Label label = new Label("将文中的换行符转换为空：");
        label.setStyle("-fx-text-fill: blue;-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 24px");
        Button btn = new Button("转换");
        btn.setFont(Font.font("微软雅黑", 24));
        btn.setOnAction(event -> {
            String textIn = textArea1.getText();
            String textOut = textIn.replaceAll(LAST_STRING, NEW_STRING);
            textArea2.setText(textOut);
        });

        HBox hBox = new HBox();
        hBox.setSpacing(10);
        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(label, btn);

        gridPane.add(hBox, 0, 0, 3, 1);

        ColumnConstraints col1 = new ColumnConstraints();
        col1.setPercentWidth(45);
        ColumnConstraints col2 = new ColumnConstraints();
        col2.setPercentWidth(10);
        ColumnConstraints col3 = new ColumnConstraints();
        col3.setPercentWidth(45);
        gridPane.getColumnConstraints().addAll(col1, col2, col3);

        Label lbl = new Label("==>");
        lbl.setFont(Font.font("微软雅黑", 32));
        gridPane.getColumnConstraints().get(1).setHalignment(HPos.CENTER);
        gridPane.addRow(1, textArea1, lbl, textArea2);

        primaryStage.setScene(new Scene(gridPane, 800, 600));
        primaryStage.setTitle("替换段落中的字符");
        primaryStage.show();
    }
}